<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" :labelWidth="120"></FormPanel>
    <FilterTable ref="table" :columns="columns" :dataSource="tableList" height="auto" :onColumnsChange="(columns) => (this.columns = columns)" :isSelectColumn="false" width="100%"> </FilterTable>
    <div style="height: 47px"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right',
      }"
    >
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 3)" :disabled="isSave">审 核 通 过</multiuse-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)" :disabled="isSave">审 核 驳 回</multiuse-button>
      <el-button @click="closeDrawer(1)" :disabled="isSave && isClose">关 闭</el-button>
    </div>
  </div>
</template>
<script>
import { getCreateDealerInfo, auditApplyPass, auditApplyReject } from '@/api/omFinanceP/RebateManage/RebateInvoiceAudit';
import config from '@/assets/js/config';
import { dictionary } from '@/utils/dictMixin';
export default {
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      columns: this.createdTable(),
      tableList: [],
      isSave: false,
      isClose: false,
      exportLoading: false,
    };
  },
  created() {
    this.createDealerInfo();
  },
  mounted() {},
  mixins: [dictionary],
  props: ['rowData'],
  methods: {
    async createDealerInfo() {
      let res = await getCreateDealerInfo(this.rowData);
      if (res.data) {
        for (let i in res.data) {
          this.formList.map((x) => {
            if (x.fieldName == i) {
              x.initialValue = res.data[i];
            }
          });
        }
        if (res.data.applyRedDetailList.length > 0) {
          this.$refs.table.EXECUTE_INSERT(res.data.applyRedDetailList);
          this.calculateOrderTotalAmount();
        }
      } else {
        this.isSave = true;
      }
    },
    async closeDrawer(type) {
      if (type === 1) {
        this.$emit('close');
      } else if (type === 2) {
        let [err, data] = await this.$refs.from.GET_FORM_DATA();
        if (err) {
          return;
        }
        let json = this.$refs.from.SUBMIT_FORM();
        if (json) {
          let apply = JSON.parse(JSON.stringify(json));
          if (apply.checkDesc == null || apply.checkDesc === '') {
            this.$notify({
              title: '警告',
              message: '审核意见不能为空',
              type: 'warning',
            });
            return;
          }
          apply.id = this.rowData.id;
          this.isSave = true;
          this.isClose = true;
          let res = await auditApplyReject(apply);
          this.isSave = false;
          this.isClose = false;
          if (res.resultCode === 200) {
            this.$notify({
              title: '成功',
              message: '审核驳回操作成功',
              type: 'success',
            });
            this.$emit('close', apply);
          } else {
            this.saveData = null;
          }
        }
      } else if (type === 3) {
        let [err, data] = await this.$refs.from.GET_FORM_DATA();
        if (err) {
          return;
        }
        let json = this.$refs.from.SUBMIT_FORM();
        if (json) {
          let apply = JSON.parse(JSON.stringify(json));
          apply.id = this.rowData.id;
          this.isSave = true;
          this.isClose = true;
          let res = await auditApplyPass(apply);
          this.isSave = false;
          this.isClose = false;
          if (res.resultCode === 200) {
            this.$notify({
              title: '成功',
              message: '审核通过操作成功',
              type: 'success',
            });
            this.$emit('close', apply);
          } else {
            this.saveData = null;
          }
        }
      } else {
        return;
      }
    },
    changeUpload(val) {
      console.log('45678');
      if (!(val && val.length > 0)) return;
      console.log(val);
      val.forEach((item) => {
        const str = item.url.split(item.name);
        if (str.length > 1) {
          item.name += str[1];
        }
      });
      console.log(val);
    },
    changeHandle(val) {
      this.saveData = val;
    },
    changeapplyAmount(index, data) {
      // console.log(index, data, '改变采购价格');
      let applyAmount = '';
      let str = '';
      for (let i in index) {
        applyAmount = index[i];
        str = i.split('|')[0];
      }
      if (applyAmount > data.afterDiscountAmount || applyAmount > 10000000000) {
        this.$notify({
          title: '警告',
          message: '输入返利红票金额不能大于采购价格',
          type: 'warning',
        });
        return;
      }
    },
    calculateOrderTotalAmount() {
      let orderTotalAmount = 0;
      this.tableList.map((x) => {
        orderTotalAmount = orderTotalAmount + Number(x.applyAmount);
      });
      this.formList.find((item) => item.fieldName == 'totalApplyAmount').initialValue = Number(orderTotalAmount).toFixed(2);
    },
    createdFrom() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '申请信息',
        },
        {
          type: 'INPUT',
          label: '返利红票申请单号',
          fieldName: 'applyNo',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '申请人',
          fieldName: 'applyPersonName',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '申请时间',
          fieldName: 'applyDate',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'applyStatus',
          disabled: true,
          placeholder: '',
          itemList: this.createDictList('2157'),
          initialValue: 21571001,
        },

        {
          type: 'INPUT_NUMBER',
          numberFormat: true,
          precision: 2,
          label: '返利可用余额',
          fieldName: 'rebateAvailableAmount',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT_NUMBER',
          numberFormat: true,
          precision: 2,
          label: '返利冲红下限',
          fieldName: 'rebateRedLowLimitAmount',
          disabled: true,
        },
        {
          type: 'INPUT_NUMBER',
          numberFormat: true,
          precision: 2,
          placeholder: '',
          rules: [{ required: true, message: '请填写大于0的金额', trigger: 'change' }],
          label: '申请金额',
          disabled: true,
          fieldName: 'totalApplyAmount',
        },
        {
          type: 'INPUT',
          label: '审批报告编号',
          disabled: true,
          fieldName: 'auditReportNo',
          placeholder: '',
        },
        {
          type: 'TEXT_AREA',
          label: '申请原因',
          fieldName: 'applyReason',
          disabled: true,
          placeholder: '',
          selfCols: 2,
        },
        {
          type: 'UPLOAD_FILE',
          label: '相关文件',
          fieldName: 'applyUidList',
          disabled: true,
          selfCols: 2,
          upload: {
            actionUrl: `${config.uploadUrl}/api/file/oss/upload`,
            fileTypes: ['jpg', 'png', 'pdf', 'xls', 'xlsx', 'rar', 'zip'],
            limit: 5,
            tipText: '最多上传5个附件',
          },
          change: (val) => {
            this.changeUpload(val);
          },
        },
        {
          type: 'BREAK_SPACE',
          label: '审核信息',
        },
        {
          type: 'INPUT',
          label: '审核人',
          fieldName: 'checkPersonName',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '审核日期',
          fieldName: 'checkDate',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'TEXT_AREA',
          label: '审核意见',
          fieldName: 'checkDesc',
          placeholder: '',
          selfCols: 2,
        },
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '结算订单编号',
          dataIndex: 'doNo',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: 'sap销售单号',
          dataIndex: 'sapNo',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '物料编码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          width: 180,
          filterType: 'input',
        },
        {
          title: '返利红票申请金额',
          dataIndex: 'applyAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 150,
          editable: false,
          editType: 'number',
          editRequired: false,
          defaultEditable: false,
          align: 'right',
          numberFormat: true,
          precision: 2,
          editPattern: /^[1-9]\d*(\.\d{0,2})?$/,
          summation: true,
          onInput: (index, data) => {
            this.changeapplyAmount(index, data);
            this.calculateOrderTotalAmount();
          },
        },
        {
          title: '采购价格',
          dataIndex: 'afterDiscountAmount',
          width: 150,
          sorter: true,
          filter: true,
          align: 'right',
          numberFormat: true,
          precision: 2,
          filterType: 'input',
        },
      ];
    },
  },
};
</script>
